package com.qqtop10.manager.utils;

import it.sauronsoftware.base64.Base64;
import java.io.UnsupportedEncodingException; 
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom; 

import javax.crypto.Cipher; 
import javax.crypto.SecretKey;  
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
 

public class EncryptionUtils
{
	private static final String guserkey="abcd1234"; 
	//private static final String guserkey="uuid3456";
    private static MessageDigest digest = null;
    
    public static final synchronized String hash(String data) {
        if (digest == null)
            try {
                digest = MessageDigest.getInstance("MD5");
            } catch (NoSuchAlgorithmException nsae) {
                System.err
                        .println("Failed to load the MD5 MessageDigest. Jive will be unable to function normally.");
                nsae.printStackTrace();
            }
        digest.update(data.getBytes());
        return toHex(digest.digest());
    }
    
    public static final String toHex(byte hash[]) {
        StringBuffer buf = new StringBuffer(hash.length * 2);
        for (int i = 0; i < hash.length; i++) {
            if ((hash[i] & 0xff) < 16)
                buf.append("0");
            buf.append(Long.toString(hash[i] & 0xff, 16));
        } 
        return buf.toString();
    }

    public static String DESEncode(String c)
    {
    	return DESEncode(c,guserkey);
    }
    public static String DESEncode(String c,String userkey)
    {
    	SecureRandom sr = new SecureRandom();
		byte[] rawKeyData = null;
		byte[] encryptedClassData =null;
		DESKeySpec dks =null;
		try {
			 rawKeyData = userkey.getBytes("UTF8");
			 dks = new DESKeySpec (rawKeyData);
			 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES" );
			 SecretKey key = keyFactory.generateSecret( dks );
			 Cipher cipher = Cipher.getInstance( "DES" );
			 cipher.init( Cipher.ENCRYPT_MODE, key, sr );
			 encryptedClassData  = cipher.doFinal(c.getBytes() );
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		String data= null;
		 try {
			 // data= new String(Base64.encode(encryptedClassData ),"UTF-8");
			data = URLEncoder.encode( new String( Base64.encode(encryptedClassData ) ) ,"UTF-8");
			// data = URLEncoder.encode( new String( encryptedClassData) ,"UTF-8");
		} catch (UnsupportedEncodingException e) { 
		//	e.printStackTrace();
		}  
		return data;
    }
    public static String DESDecode(String c)
    {
    	return DESDecode(c,guserkey);
    }
    public static String DESDecode(String c,String userkey)
    {
    	 
    	SecureRandom sr = new SecureRandom();
		byte[] rawKeyData = null;
		byte[] encryptedClassData =null;
		DESKeySpec dks =null;
		try {
			
			byte[] decx = URLDecoder.decode(c,"UTF-8").getBytes();// .decode(Base64.decode(c).getBytes(),"UTF-8").getBytes();//;
			 //byte[] dec = Base64.decode( c.getBytes("UTF-8") );
			byte[] dec = Base64.decode( decx);
			 rawKeyData = userkey.getBytes( );
			 dks = new DESKeySpec (rawKeyData);
			 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES" );
			 SecretKey key = keyFactory.generateSecret( dks );
			 Cipher cipher = Cipher.getInstance( "DES" );
			 cipher.init( Cipher.DECRYPT_MODE, key, sr );
			 encryptedClassData  = cipher.doFinal(dec); 
		} catch (Exception e) { 
			//e.printStackTrace();
		}
		return new String(encryptedClassData);
    }
    
    
  public static void main(String[] s)
  {
	  //String t= hash("EURUSD201405280800abc");
	  //System.out.println(t);
	  System.out.println( EncryptionUtils.DESEncode("1860044671"));
	  System.out.println( EncryptionUtils.DESDecode("HjayFqc0ApF%2Bdo2yVPdQ9Q%3D%3D"));
	  
  }
}
